'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, INTEGER, STRING, TEXT, DATE, ENUM } = app.Sequelize;

  const Article = model.define(
    'Article',
    {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      category_id: {
        type: UUID,
        allowNull: false,
      },
      title: {
        type: STRING(50),
        allowNull: false,
      },
      cover: {
        type: STRING(255),
        allowNull: true,
      },
      abstract: {
        type: STRING(255),
        allowNull: false,
      },
      content: {
        type: TEXT,
        allowNull: false,
      },
      keywords: {
        type: STRING(255),
        allowNull: true,
      },
      tags: {
        type: STRING(255),
        allowNull: true,
      },
      source: {
        type: STRING(255),
        allowNull: true,
      },
      url: {
        type: STRING(255),
        allowNull: false,
      },
      published_at: {
        type: DATE,
        allowNull: false,
      },
      status: {
        type: ENUM,
        values: [ '0', '1' ],
        defaultValue: '0',
        comment: '0 未发布 1 已发布',
      },
    },
    {
      tableName: 'cms_article',
    }
  );

  Article.associate = function() {
    const { Cms: { Article, Category } } = model;
    Article.belongsTo(Category, { as: 'category', foreignKey: 'category_id' });
  };

  return Article;
};
